layui.define(["form","jquery"],function(exports){
    var form = layui.form,
    $ = layui.jquery,
    Address = {
        provinces : function(province,city,area) {
            //加载省数据
            var proHtml = '',that = this;
            $.get("/shop/static/layui/json/address.json", function (data) {
                for (var i = 0; i < data.length; i++) {
                	if(i ==province){
                        proHtml += '<option  selected = "selected"  value="' + i + '">' + data[i].name + '</option>';
                	}else{
                        proHtml += '<option value="' + i + '">' + data[i].name + '</option>';
                	}   	
                }
                //初始化省数据
                $("select[name=province]").append(proHtml);
                form.render();
                if(city!='-1'){
                	var citys = data[province].childs;
                	that.citys(citys,city);
                	if(area!='-1'){
                		 that.areas(citys[city].childs,area);
                	}
                }
                form.on('select(province)', function (proData) {
                    $("select[name=area]").html('<option value="">请选择县/区</option>');
                    var value = proData.value;
                    if (value > 0) {
                        that.citys(data[$(this).index() - 1].childs);
                    } else {
                        $("select[name=city]").attr("disabled", "disabled");
                    }
                });
            })
        },
        //加载市数据
        citys : function(citys,value) {
            var cityHtml = '<option value="">请选择市</option>',that = this;
            for (var i = 0; i < citys.length; i++) {
            	if(i==value){
                    cityHtml += '<option selected = "selected" value="' + i + '">' + citys[i].name + '</option>';
            	}else{
                    cityHtml += '<option value="' + i + '">' + citys[i].name + '</option>';
            	}
            	
            }
            $("select[name=city]").html(cityHtml).removeAttr("disabled");
            form.render();
            form.on('select(city)', function (cityData) {
                var value = cityData.value;
                if (value > 0) {
                    that.areas(citys[$(this).index() - 1].childs);
                } else {
                    $("select[name=area]").attr("disabled", "disabled");
                }
            });
        },
        //加载县/区数据
        areas : function(areas,value) {
            var areaHtml = '<option value="">请选择县/区</option>';
            for (var i = 0; i < areas.length; i++) {
            	if(i==value){
                    areaHtml += '<option selected = "selected" value="' + i + '">' + areas[i].name + '</option>';
            	}else{
                    areaHtml += '<option value="' + i + '">' + areas[i].name + '</option>';
            	}
            }
            $("select[name=area]").html(areaHtml).removeAttr("disabled");
            form.render();
        }
    };
    exports("address",Address);
})